#include <REGX52.H>

#define BUTTON1 P3_2
#define BUTTON2 P2_6
#define u8 unsigned char

//数码管段码表
unsigned char code NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char Sec=55,Min=59,Hour=23;//设置时间
int flag = 1;//设置闹钟的标志位
unsigned char Sec_S=0,Min_S=0,Hour_S=0;//闹钟响铃的时间

void Delay(unsigned int xms);//延时,单位毫秒
void Nixie(unsigned char Location,Number);//数码管显示，第一个参数位，第二个参数所要显示的数字
void Timer0Init(void);//定时器初始化

void Time_display(u8 Hour,u8 Min,u8 Sec);//显示时间
void LED(int state);//LED开关,1为开，0为关
void Set_AlarmClock(void);//设置闹钟
char Read_Button(void);//返回0表示第一个按键按下，返回1表示没有按键按下，返回2表示第二个按键按下


void main()
{
	int alarmClock = 0;
	Timer0Init();
	while(1)
	{
		//读取按键
		flag = Read_Button();
		
		//显示时间设置时间
		if(flag)
			Time_display(Hour,Min,Sec);
		else
			Set_AlarmClock();
		
		//闹钟相关
		if(Hour_S == Hour && Min_S == Min && Sec_S == Sec)
		{
			LED(1);
			alarmClock = 1;
		}
		if(alarmClock)
		{
			alarmClock++;
			if(alarmClock == 100)
			{
				alarmClock = 0;
				LED(0);
			}
		}
	}
}


//延时子函数
void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

//数码管显示子函数
void Nixie(unsigned char Location,Number)
{
	switch(Location)		//位码输出
	{
		case 1:P2_0=1;P2_1=0;P2_2=0;P2_3=0;P2_4=0;P2_5=0;break;
		case 2:P2_0=0;P2_1=1;P2_2=0;P2_3=0;P2_4=0;P2_5=0;break;
		case 3:P2_0=0;P2_1=0;P2_2=1;P2_3=0;P2_4=0;P2_5=0;break;
		case 4:P2_0=0;P2_1=0;P2_2=0;P2_3=1;P2_4=0;P2_5=0;break;
		case 5:P2_0=0;P2_1=0;P2_2=0;P2_3=0;P2_4=1;P2_5=0;break;
		case 6:P2_0=0;P2_1=0;P2_2=0;P2_3=0;P2_4=0;P2_5=1;break;
	}
	P0=~NixieTable[Number];	//段码输出
	Delay(1);				//显示一段时间
	P0=0xFF;				//段码清0，消影
}

void LED(int state)
{
	P1_0 = !state;
}

void Time_display(u8 Hour,u8 Min,u8 Sec)
{
	Nixie(1,Hour/10);
	Nixie(2,Hour%10);//显示时
	
	Nixie(3,Min/10);
	Nixie(4,Min%10);//显示时
	
	Nixie(5,Sec/10);
	Nixie(6,Sec%10);//显示时
}

void Timer0Init(void)
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
	PT0=0;
}

char Read_Button(void)
{
		if(BUTTON1 == 0)
		{
			Delay(100);
			while(BUTTON1 == 0);
			//Delay(100);
			return 0;
		}
		else if(BUTTON2 == 0)
		{
			Delay(100);
			while(BUTTON1 == 0);
			//Delay(100);
			return 2;
		}
		return 1;
}

void Set_AlarmClock(void)
{
	char cnt = 0;
	char num = 0;
	char Key = 0;
	unsigned char time[] = {0,0,0,0,0,0};
	P0=0xFF;
	P2_0=0;P2_1=0;P2_2=0;P2_3=0;P2_4=0;P2_5=0;
	while(cnt < 6)
	{
		Key = Read_Button();
		if(Key == 0)
		{
			cnt++;
			num = 0;
		}
		if(Key == 2)
		{
			switch(cnt)
			{
				case 0: 
					num++;
					num%=3;
					time[0] = num;
					break;
				case 1:
					num++;
					if(time[0] == 2)
						num%=5;
					else
						num%=10;
					time[1] = num;
					break;
				case 2:
					if(Hour_S == 24)
						break;
					num++;
					num%=7;
					time[2] = num;
					break;
				case 3:
					if(Hour_S == 24)
						break;
					num++;
					if(time[2] == 6)
					{
						time[3] = 0;
						break;
					}
					num%=10;
					time[3] = num;
					break;
				case 4:
					if(Hour_S == 24)
						break;
					num++;
					num%=7;
					time[4] = num;
					break;
				case 5:
					if(Hour_S == 24)
						break;
					num++;
					if(time[4] == 6)
					{
						time[5] = 0;
						break;
					}
					num%=10;
					time[5] = num;
					break;
			}
		}
		Hour_S = time[0] * 10;
		Hour_S += time[1];
		
		Min_S = time[2] * 10;
		Min_S += time[3];
		
		Sec_S = time[4] * 10;
		Sec_S += time[5];
		Time_display(Hour_S,Min_S,Sec_S);
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	T0Count++;
	if(T0Count>=1000)	//定时器分频，1s
	{
		T0Count=0;
		Sec++;			//1秒到，Sec自增
		if(Sec>=60)
		{
			Sec=0;		//60秒到，Sec清0，Min自增
			Min++;
			if(Min>=60)
			{
				Min=0;	//60分钟到，Min清0，Hour自增
				Hour++;
				if(Hour>=24)
				{
					Hour=0;	//24小时到，Hour清0
				}
			}
		}
	}
}


